package hng.service.admin.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import hng.dto.admin.*;
import hng.dto.store.CommentQuery;
import hng.dto.store.OrdersQuery;
import hng.dto.store.ProductQuery;
import hng.enums.UserRoleEnum;
import hng.mapper.*;
import hng.model.OrdersDO;
import hng.model.ProductDO;
import hng.model.SysInfoDO;
import hng.model.UserDO;
import hng.service.UserService;
import hng.service.admin.AdminService;
import hng.util.CommonUtil;
import hng.vo.ProductCommentVO;
import hng.vo.admin.*;
import hng.vo.store.OrdersVO;
import hng.vo.store.PageResult;
import org.apache.commons.codec.digest.Md5Crypt;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 轮播图表 服务实现类
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private UserService userService;

    @Autowired(required = false)
    private StoreMapper storeMapper;

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Autowired(required = false)
    private BannerMapper bannerMapper;

    @Autowired(required = false)
    private ProductCommentMapper commentMapper;

    @Autowired(required = false)
    private OrdersMapper ordersMapper;

    @Autowired(required = false)
    private NavMapper navMapper;

    @Autowired(required = false)
    private SysInfoMapper sysInfoMapper;

    /**
     * 查询用户列表
     */
    @Override
    public PageResult userList(UserQuery userQuery) {
        Page<Object> objects = PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        List<UserDetailVO> userQueryList = userMapper.userByAdmin();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, userQueryList);
        return pageResult;
    }

    /**
     * 查询管理员列表
     */
    @Override
    public PageResult adminList(UserQuery userQuery) {
        Page<Object> objects = PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());
        List<UserDetailVO> userQueryList = userMapper.adminByAdmin();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, userQueryList);
        return pageResult;
    }

    /**
     * excel批量导入管理员
     */
    @Override
    public void importAdmin(MultipartFile file) throws IOException {
        List<UserDO> userList = new ArrayList<>();
        InputStream inputStream = file.getInputStream();
        // 读取Excel文件
        EasyExcel.read(inputStream, AdminExcelDTO.class, new AnalysisEventListener<AdminExcelDTO>() {
            @Override
            public void invoke(AdminExcelDTO data, AnalysisContext context) {
                // 当每次读取到一条数据时会调用这个方法
                UserDO userDO = new UserDO();
                BeanUtils.copyProperties(data, userDO);
                // 设置盐和密码
                userDO.setRole(UserRoleEnum.ADMIN.getCode());
                userDO.setSecret("$1$" + CommonUtil.getStringRandom(8));
                userDO.setPassword(Md5Crypt.md5Crypt(data.getPassword().getBytes(), userDO.getSecret()));
                userList.add(userDO);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 全部读取完成后会调用这个方法
                System.out.println("All data is parsed.");
                // 保存数据到数据库
                userService.saveBatch(userList);
                System.out.println("Data saved to database.");
            }
        }).sheet().doRead();
        // 关闭输入流
        inputStream.close();
    }

    /**
     * 分页查询商品列表
     */
    @Override
    public PageResult productList(ProductQuery productQuery) {
        Page<Object> objects = PageHelper.startPage(productQuery.getPageNum(), productQuery.getPageSize());
        List<ProductDO> productQueryList = productMapper.productByAdmin();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, productQueryList);
        return pageResult;
    }

    /**
     * 分页查询评论列表
     */
    @Override
    public PageResult commentList(CommentQuery commentQuery) {
        Page<Object> objects = PageHelper.startPage(commentQuery.getPageNum(), commentQuery.getPageSize());
        List<ProductCommentVO> productQueryList = commentMapper.getListByAdmin(commentQuery.getKeyword());
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, productQueryList);
        return pageResult;
    }

    /**
     * 分页查询订单列表
     */
    @Override
    public PageResult ordersList(OrdersQuery ordersQuery) {
        Page<Object> objects = PageHelper.startPage(ordersQuery.getPageNum(), ordersQuery.getPageSize());
        List<OrdersVO> ordersVOList = ordersMapper.getListByAdmin();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, ordersVOList);
        return pageResult;
    }

    /**
     * 分页查询店铺列表
     */
    @Override
    public PageResult storeList(StoreQuery storeQuery) {
        Page<Object> objects = PageHelper.startPage(storeQuery.getPageNum(), storeQuery.getPageSize());
        List<StoreDetailVO> storeList = storeMapper.getStoreListByAdmin(storeQuery.getKeyword());
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, storeList);
        return pageResult;
    }

    /**
     * 分页查询轮播图列表
     */
    @Override
    public PageResult bannerList(BannerQuery bannerQuery) {
        Page<Object> objects = PageHelper.startPage(bannerQuery.getPageNum(), bannerQuery.getPageSize());
        List<BannerVO> storeList = bannerMapper.bannerList();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, storeList);
        return pageResult;
    }

    /**
     * 分页查询导航栏列表
     */
    @Override
    public PageResult navList(NavQuery navQuery) {
        Page<Object> objects = PageHelper.startPage(navQuery.getPageNum(), navQuery.getPageSize());
        List<NavVO> navList = navMapper.navList();
        long size = objects.getTotal();
        PageResult pageResult = new PageResult(size, navList);
        return pageResult;
    }

    /**
     * 网站信息
     */
    @Override
    public List<SysInfoDO> sysInfo() {
        List<SysInfoDO> infoDOList = sysInfoMapper.selectList(null);
        return infoDOList;
    }

    /**
     * 获取首页4个数据
     */
    @Override
    public TotalDataVO totalData() {
        // 用户总数量
        Integer userCount = userMapper.selectCount(null);
        // 商家总数量
        Integer storeCount = storeMapper.selectCount(null);
        // 获取店铺商品总数量
        Integer productCount = productMapper.selectCount(null);
        // 交易数总价
        Double moneyCount = ordersMapper.moneyTotalByAdmin();

        TotalDataVO totalDataVO = new TotalDataVO();
        totalDataVO.setUserTotal(userCount);
        totalDataVO.setStoreTotal(storeCount);
        totalDataVO.setProductTotal(productCount);
        totalDataVO.setMoneyTotal(moneyCount);
        return totalDataVO;
    }
}
